/// 万年历日期信息模型 - Calendar day info model
/// 
/// 用于存储万年历中单个日期的详细信息
/// Used to store detailed information for a single date in the calendar
class CalendarDayInfo {
  final int solarYear;
  final int solarMonth;
  final int solarDay;
  final int lunarYear;
  final String lunarMonth;
  final String lunarDay;
  final String weekday;
  final String constellation;
  final String chineseZodiac;
  final String ganZhi;
  final String? solarTerm;
  final List<String> festivals;
  final List<String> lunarFestivals;
  final List<String> suitable;
  final List<String> avoid;
  final String fiveElements;
  final String nayin;
  final String chong;
  final String sha;
  final List<String> jishen;
  final List<String> xiongshen;

  const CalendarDayInfo({
    required this.solarYear,
    required this.solarMonth,
    required this.solarDay,
    required this.lunarYear,
    required this.lunarMonth,
    required this.lunarDay,
    required this.weekday,
    required this.constellation,
    required this.chineseZodiac,
    required this.ganZhi,
    this.solarTerm,
    this.festivals = const [],
    this.lunarFestivals = const [],
    this.suitable = const [],
    this.avoid = const [],
    required this.fiveElements,
    required this.nayin,
    required this.chong,
    required this.sha,
    this.jishen = const [],
    this.xiongshen = const [],
  });

  /// 从JSON创建实例 - Create instance from JSON
  factory CalendarDayInfo.fromJson(Map<String, dynamic> json) {
    return CalendarDayInfo(
      solarYear: json['solar_year'] ?? 0,
      solarMonth: json['solar_month'] ?? 0,
      solarDay: json['solar_day'] ?? 0,
      lunarYear: json['lunar_year'] ?? 0,
      lunarMonth: json['lunar_month'] ?? '',
      lunarDay: json['lunar_day'] ?? '',
      weekday: json['weekday'] ?? '',
      constellation: json['constellation'] ?? '',
      chineseZodiac: json['chinese_zodiac'] ?? '',
      ganZhi: json['gan_zhi'] ?? '',
      solarTerm: json['solar_term'],
      festivals: List<String>.from(json['festivals'] ?? []),
      lunarFestivals: List<String>.from(json['lunar_festivals'] ?? []),
      suitable: List<String>.from(json['suitable'] ?? []),
      avoid: List<String>.from(json['avoid'] ?? []),
      fiveElements: json['five_elements'] ?? '',
      nayin: json['nayin'] ?? '',
      chong: json['chong'] ?? '',
      sha: json['sha'] ?? '',
      jishen: List<String>.from(json['jishen'] ?? []),
      xiongshen: List<String>.from(json['xiongshen'] ?? []),
    );
  }

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'solar_year': solarYear,
      'solar_month': solarMonth,
      'solar_day': solarDay,
      'lunar_year': lunarYear,
      'lunar_month': lunarMonth,
      'lunar_day': lunarDay,
      'weekday': weekday,
      'constellation': constellation,
      'chinese_zodiac': chineseZodiac,
      'gan_zhi': ganZhi,
      'solar_term': solarTerm,
      'festivals': festivals,
      'lunar_festivals': lunarFestivals,
      'suitable': suitable,
      'avoid': avoid,
      'five_elements': fiveElements,
      'nayin': nayin,
      'chong': chong,
      'sha': sha,
      'jishen': jishen,
      'xiongshen': xiongshen,
    };
  }

  /// 是否有节日 - Has festivals
  bool get hasFestivals => festivals.isNotEmpty || lunarFestivals.isNotEmpty;

  /// 是否有节气 - Has solar term
  bool get hasSolarTerm => solarTerm != null && solarTerm!.isNotEmpty;

  /// 所有节日 - All festivals
  List<String> get allFestivals => [...festivals, ...lunarFestivals];

  /// 宜事文本 - Suitable text
  String get suitableText => suitable.isEmpty ? '无' : suitable.join('、');

  /// 忌事文本 - Avoid text
  String get avoidText => avoid.isEmpty ? '无' : avoid.join('、');

  /// 农历日期文本 - Lunar date text
  String get lunarDateText {
    if (lunarDay == '初一') {
      return lunarMonth;
    }
    return lunarDay;
  }
}

/// 月视图数据模型 - Month view data model
class MonthViewData {
  final int year;
  final int month;
  final List<CalendarDayInfo> days;

  const MonthViewData({
    required this.year,
    required this.month,
    required this.days,
  });

  /// 从JSON创建实例 - Create instance from JSON
  factory MonthViewData.fromJson(Map<String, dynamic> json) {
    return MonthViewData(
      year: json['year'] ?? 0,
      month: json['month'] ?? 0,
      days: (json['days'] as List<dynamic>?)
          ?.map((day) => CalendarDayInfo.fromJson(day))
          .toList() ?? [],
    );
  }

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'year': year,
      'month': month,
      'days': days.map((day) => day.toJson()).toList(),
    };
  }
}